<?php

class DMembers extends DActiveRecord {

    private $curPage = 0;
    private $defaultSort;
    public $new_password;
    public $re_password;
    
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'members';
    }

    public function rules() {
        return array(
            array('email, password, full_name',
                'required'),
            array('email', 'email'),
            array('email', 'ext.validators.DupplicateValidator',
                'tableName'   => $this->tableName(),
                'isNewRecord' => $this->isNewRecord,
                'currId'      => $this->id,
            ),
            array('activated_flag',
                'numerical', 'integerOnly' => true),
            array('re_password',
                'compare',
                'on'               => 'insert, update',
                'compareAttribute' => 'password',
                'message'          => Yii::t('DadminModule.message', 'were_not_match', array(
                    '{0}' => $this->getAttributeLabel('password'),
                    '{1}' => $this->getAttributeLabel('re_password'),
                )),
            ),
            array('email, full_name, address, job',
                'length', 'max' => 255),
            array('password, phone', 'length', 'max' => 32),
            array('last_login', 'safe'),
            
            array('email, full_name, address, phone, job, activated_flag,  last_login',
                'safe', 'on' => 'search'),
        );
    }

    public function relations() {
        return array();
    }

    public function attributeLabels() {
        return array(
            'id'                => 'ID',
            'email'             => 'Email',
            'password'          => 'Password',
            're_password'       => 'Retype Password',
            'full_name'         => 'Full Name',
            'address'           => 'Address',
            'phone'             => 'Phone',
            'job'               => 'Job',
            'activated_flag'    => 'Active',
            'confirmation_code' => 'Confirmation Code',
            'last_login'        => 'Last Login',
        );
    }

    public function search() {
        //Restore state from session
        $this->restoreState();

        //Criteria
        $criteria = new CDbCriteria;
        $criteria->compare('full_name', $this->full_name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('activated_flag', $this->activated_flag);

        //Sorting
        $sort               = new CSort();
        $sort->defaultOrder = ($this->defaultSort == '') ?
            array('id'=> true)
            :
            $this->defaultSort;
        
        $sort->attributes = array(
            'id' => array(
                'asc'       => 'id ASC',
                'desc'      => 'id DESC',
                'default'   => 'desc',
            ),
            'full_name' => array(
                'asc'      => 'full_name ASC',
                'desc'     => 'full_name DESC',
                'default'  => 'asc',
            ),
            'email' => array(
                'asc'     => 'email ASC',
                'desc'    => 'email DESC',
                'default' => 'asc',
            ),
            'phone'   => array(
                'asc'            => 'phone ASC',
                'desc'           => 'phone DESC',
            ),
            'activated_flag' => array(
                'asc'     => 'activated_flag ASC',
                'desc'    => 'activated_flag DESC',
                'default' => 'asc',
            ),
        );
        $sort->applyOrder($criteria);

        return new CActiveDataProvider($this, array(
            'criteria'   => $criteria,
            'sort'       => $sort,
            'pagination' => array('currentPage' => $this->curPage),
        ));
    }

    private function restoreState() {
        $state = SpsState::getState();

        if ($state) {
            if (isset($state['sort']))
                $this->defaultSort = $state['sort'];
            if (isset($state['page']))
                $this->curPage     = $state['page'];

            if (isset($state['search'])) {
                foreach ($state['search'] as $key => $value)
                    if ($value != '')
                        $this->$key = $value;
            }
        }
    }
    
    protected function beforeSave() {
        parent::beforeSave();

        if ($this->getIsNewRecord()) {
            $this->password = $this->encrypt($this->password);
        } else {
            $oldRow = $this->findByPk($this->id);
            if ($oldRow->password != $this->password)
                $this->password = $this->encrypt($this->password);
        }

        return true;
    }
    
    private function encrypt($password) {
        return md5($password);
    }
    
}